package phone.book;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.Toast;

public class export_contacts extends ListActivity{

	ArrayList<String> nlist;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		nlist = new ArrayList<String>();
		ContentResolver cr = getContentResolver();
		
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,    null, null, null, null);
        
        if (cur.getCount() > 0) {
	        
        	while (cur.moveToNext()) {
        	Long id = Long.parseLong(cur.getString(cur.getColumnIndex(BaseColumns._ID)));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));           
            nlist.add(name);
            
            String hasPhoneNumber = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if (Integer.parseInt(hasPhoneNumber) > 0) {
                      
                     // Build the Uri to query to table
                   // Query the table
                     cur = getContentResolver().query(Phone.CONTENT_URI,
                             null, Phone.CONTACT_ID + "=?", new String[] {cur.getString(cur.getColumnIndex(BaseColumns._ID)) },
                             null);

                     int PhoneIdx = cur.getColumnIndex(Phone.DATA);
                    
                     // Get the phone numbers from the contact
                     if (cur.moveToFirst()) {
                          
                         // Get a phone number
                         String phoneNumber =cur.getString(PhoneIdx);
                        // int type = cur.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_ITEM_TYPE);;
                   //      tv2.setText(phoneNumber);
                      //   contactsList.add(phoneNumber);
                     //   tv3.setText(mail);
         
                         
                     }
                 } else {
                  Toast.makeText(this, "This contact doesn't have a phone number",
                             Toast.LENGTH_LONG).show();
                 }
            
        	}
        }
	}
}
